<?php
namespace app\wechat\controller;

use think\Controller;
use app\wechat\middlewares\LoginMiddlewares;
use app\wechat\middlewares\Response;
use app\service\Wechat;

class Login extends Controller{
    public function weChatAccredit(){
        $url = 'http://'.$_SERVER['HTTP_HOST'].'/wechat-middle'; //跳转地址
        $weChat = new Wechat();
        $weChat->accredit($url);
    }

    public function middle(){
        try{
            $weChat = new Wechat();
            $code = $_GET['code'];
            $access_token = $weChat->getAccessToken($code);
            //根据access_token获取用户信息
            $weChatUserInfo = $weChat->getWeChatUserInfo($access_token['access_token'],$access_token['openid']);
            $result = LoginMiddlewares::middleFactory($weChatUserInfo);
            $url = 'http://'.$_SERVER['HTTP_HOST'].'/html-index?openid='.$weChatUserInfo['openid'].'&nickname='.$result['nickname'].'&avatar='.$result['avatar'].'&integral='.$result['integral'].'&grade='.$result['grade'].'&validity='.$result['validity'].'&sign='.$result['sign'].'&createAt='.$result['createAt'].'&uid='.$result['uid']; //跳转地址
            $this->redirect($url,302);
        }catch(\LogicException $e){
            return Response::data($e->getCode(),$e->getMessage());
        }
    }

//    public function bindPhoneNum(){
//        try{
//            $params = input('param.');
//            $result = LoginMiddlewares::bindFactory($params);
//            if($result['code'] != 200){
//                throw new \LogicException($result['errmsg'],$result['code']);
//            }
//            return Response::data($result['code'],$result['errmsg']);
//        }catch(\LogicException $e){
//            return Response::data($e->getCode(),$e->getMessage());
//        }
//    }
//    public function login(){
//        try{
//            $params = input('param.');
//            $result = LoginMiddlewares::loginFactory($params);
//            if($result['code'] != 200){
//                throw new \LogicException($result['errmsg'],$result['code']);
//            }
//            cookie('login',$result['id'],3600 * 7);
//            return Response::data($result['code'],$result['errmsg'],$result['data']);
//        } catch (\LogicException $e){
//            return Response::data($e->getCode(),$e->getMessage());
//        }
//    }
}